exports.install = function (Vue, options) {
    
    Vue.prototype.domain = 'http://local.api.queue.com/',

    Vue.prototype.apis = {
        captcha: 'site/captcha',
        guid: 'site/guid',
        sendsms: 'members/send-verify-sms',
        login: 'members/login',
        checkin: 'members/check-in',
        register: 'members/register',
        createQue: 'queues/que',
        myQueue: 'queues/my-que'
    },
    
    Vue.prototype.headers = {},

    /**
     * 跳转
     * @param string url 
     * @param string params 
     */
    Vue.prototype.goUrl = function (url, params) {
        this.$router.push({path: url, query: params})
    },

    /**
     * 判断登录状态
     */
    Vue.prototype.isLogin = function () {
        if (!this.$state.isLogin) {
            this.goUrl('login')
        }
    },

    /**
     * 获取IP地址
     */
    Vue.prototype.getApi = function (apiName) {
        let api = this.domain + this.apis[apiName]
        return api
    },

    /**
     * 获取要提交的http信息头
     */
    Vue.prototype.getHeaders = function (apiName) {
        this.headers['Content-Type'] = 'application/x-www-form-urlencoded'
        this.headers['guid'] = this.$cookies.get('guid')
        this.headers['token'] = this.$cookies.get('token')
    },

    /**
     * 获取GUID
     */
    Vue.prototype.getGuid = function () {
        let ts = this
        let guid = this.$cookies.get('guid')
        if (guid == '' || guid == null) {
            let api = this.getApi('guid')
            this.$ajax({
                method: 'POST',
                url: api,
            }).then(function (res) {
                let status = res.data.code
                if (status) {
                    ts.$cookies.set('guid', res.data.result.guid, 365 * 86400 * 100)
                    this.goUrl('index')
                } else {
                    ts.loading = '获取GUID失败'
                }
            }).catch(function (error){
                console.log(error)
            })
        }else{
            this.goUrl('index')
        }
    },

    /**
     * 获取图片验证码
     */
    Vue.prototype.getCaptcha = function () {
        this.getHeaders()
        let ts = this
        let api = this.getApi('captcha')
        this.$ajax({
            method: 'POST',
            url: api,
            data: {},
            headers: this.headers,
        }).then(function(res){
            ts.captchaCode = res.data.result.img
        }).catch(function(error){
            console.log(error)
        })
    },

    /**
     * 获取Form提交的数据
     */
    Vue.prototype.getFormData = function (data) {
        let arr = []
        let i = 0
        for(let k in data){
            arr[i] = k + '=' + data[k]
            i++
        } 
        return arr.join('&')
    },

    /**
     * 检查是否做过登记
     */
    Vue.prototype.checkIn = function () {
        this.getHeaders()
        let ts = this
        let api = this.getApi('checkin')
        this.$ajax({
            method: 'POST',
            url: api,
            data: {},
            headers: this.headers,
        }).then(function(res){
            if(res.data.code == 0){
                ts.goUrl('checkin')
            }else{
                ts.createQue();
            }
        }).catch(function(error){
            console.log(error)
        })
    }
    
}